package com.cloudcc.mobile.im.ui;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.dialog.BackTrueDialog;
import com.cloudcc.mobile.im.Constant;
import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupManager;
import com.easemob.easeui.widget.EaseAlertDialog;
import com.easemob.exceptions.EaseMobException;
import com.litesuits.android.log.Log;

import java.util.List;

/**
 * 创建群组
 * 
 * @Description:
 * @author wangzhiqiang
 * @date 2015-10-15 下午2:02:46
 * 
 */
public class NewGroupActivity extends BaseActivity implements BackTrueDialog.BackTrue {
	private EditText groupNameEditText;
	private ProgressDialog progressDialog;
	private EditText introductionEditText;
	private CheckBox checkBox;
	private CheckBox memberCheckbox;
	private LinearLayout openInviteContainer;
	protected List<EMGroup> grouplist;
    private String groupName;
    private String namegx;
	private BackTrueDialog diagox;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.em_activity_new_group);
		groupNameEditText = (EditText) findViewById(R.id.edit_group_name);
		introductionEditText = (EditText) findViewById(R.id.edit_group_introduction);
		checkBox = (CheckBox) findViewById(R.id.cb_public);
		memberCheckbox = (CheckBox) findViewById(R.id.cb_member_inviter);
		openInviteContainer = (LinearLayout) findViewById(R.id.ll_open_invite);

		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					openInviteContainer.setVisibility(View.INVISIBLE);
				} else {
					openInviteContainer.setVisibility(View.VISIBLE);
				}
			}
		});
	}

	/**
	 * @param v
	 */
	public void save(View v) {
		String name = groupNameEditText.getText().toString();
		if (TextUtils.isEmpty(name)) {
			new EaseAlertDialog(this, R.string.Group_name_cannot_be_empty)
					.show();
		} else if(name.length()<2) {
			diagox=	new BackTrueDialog(this, R.style.DialogLoadingTheme,R.string.lest_length,R.string.ok);
			diagox.show();
		}else {
			// 进通讯录选人
			startActivityForResult(
					new Intent(this, GroupPickContactsActivity.class).putExtra(
							"groupName", name), 0);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			final Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		String st1 = getResources().getString(
				R.string.Is_to_create_a_group_chat);
		final String st2 = getResources().getString(
				R.string.Failed_to_create_groups);
		if (resultCode == RESULT_OK) {
			// 新建群组
			progressDialog = new ProgressDialog(this);
			progressDialog.setMessage(st1);
			progressDialog.setCanceledOnTouchOutside(false);
			progressDialog.show();

			new Thread(new Runnable() {
				@Override
				public void run() {
					// 调用sdk创建群组方法
					 groupName = groupNameEditText.getText().toString()
							.trim();
					String desc = introductionEditText.getText().toString();
					String[] members = data.getStringArrayExtra("newmembers");
					try {
						// if(checkBox.isChecked()){
						// 创建公开群，此种方式创建的群，可以自由加入
						// 创建公开群，此种方式创建的群，用户需要申请，等群主同意后才能加入此群
						if ("".equals(desc)) {
							desc = "暂无简介";
						}
						Log.d("getLocalizedMessage", groupName + "::" + desc
								+ "::" + members);
						EMGroupManager.getInstance().createPublicGroup(
								groupName, desc, members, true, 200);
						// }else{
						// 创建不公开群
						// EMGroupManager.getInstance().createPrivateGroup(groupName,
						// desc, members, memberCheckbox.isChecked(),200);
						// }
						runOnUiThread(new Runnable() {
							public void run() {
								progressDialog.dismiss();
								setResult(RESULT_OK);
                                grouplist = EMGroupManager.getInstance().getAllGroups();
                                for (int i = 0;i<grouplist.size();i++){

                                     namegx = grouplist.get(i).getName();
                                    if(groupName.equals(namegx)) {
                                        Intent intent = new Intent(mContext,
                                                ChatActivity.class);
                                        intent.putExtra("chatType", Constant.CHATTYPE_GROUP);
                                        intent.putExtra("userId", grouplist.get(i).getGroupId());
                                        startActivityForResult(intent, 0);
                                    }
                                }
								finish();

							}
						});
					} catch (final EaseMobException e) {
						runOnUiThread(new Runnable() {
							public void run() {
								progressDialog.dismiss();
								Toast.makeText(NewGroupActivity.this,
										st2 , 1)
										.show();
								Log.d("getLocalizedMessage",
										e.getLocalizedMessage());
							}
						});
					}

				}
			}).start();
		}
	}

	public void back(View view) {
		finish();
	}

	@Override
	public void rightBt() {
		diagox.dismiss();
	}
}
